<?php
/**
 * this file has one class Conf
 *
 * PHP version 5
 *
 * @category Config
 * @package  Conf
 * @author   sink <sink.cup@gmail.com>
 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://pear.php.net/package/Conf/
 */

require_once 'Conf/Exception.php';

/**
 * use this class to parse a config file
 *
 * now it supports one format 'php_array'
 *
 * Demo:
 * if the config file is /data/conf/db.conf, content is php array:
 * <code>
 * <?php
 * $mysql = array(
 *     'db_name' => 'example',
 *     'username' => 'root',
 *     'password' => 1,
 * )
 * ?>
 * </code>
 * you can parse it like this:
 * <code>
 * <?php
 * require_once('Conf.php');
 * $mysql_conf = Conf::get('/data/conf/', 'db', 'mysql');
 * ?>
 * </code>
 *
 * Because the conf_dir never change, you write your class like this:
 * <code>
 * <?php
 * class ConfLib
 * {
 *     private function __construct()
 *     {
 *     }
 * 
 *     public static function get($file_path, $key) {
 *         $conf_dir = dirname(__FILE__) . '/../../conf/';
 *         return Conf::get($conf_dir, $file_path, $key);
 *     }
 * }
 * ?>
 * </code>
 *
 * @category Config
 * @package  Conf
 * @author   sink <sink.cup@gmail.com>
 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://pear.php.net/package/Conf/
 */
class Conf
{
    /**
     * forbid new class
     */
    private function __construct()
    {
    }

    /**
     * get conf from a file
     *
     * @param string $conf_dir  eg. '/data/conf/'
     * @param string $file_path eg. 'db' or 'db/mysql'
     * @param string $key       eg. 'server' or 'server[\'pgsql\'][\'db_name\']'
     * @param string $conf      file_ext default is '.conf',
     *                          format default is 'php_array'
     *
     * @return   mixed
     * @throws   Conf_Exception
     */
    public static function get($conf_dir, $file_path, $key, 
        $conf = array(
            'file_ext' => '.conf',
            'format'   => 'php_array',
        )
    ) {
        require $conf_dir . $file_path . $conf['file_ext'];
        /* eg. $server = array(
            'pgsql' => array(
                'db_name' => 'example',
                'username' => 'root',
            )
        )
        */
        $tmp         = str_replace(array('[\'', '\'][\'', '\']'), array('->'), $key);
        /* eg. $key = 'server[\'pgsql\'][\'db_name\']';
         $tmp = 'server->pgsql->db_name';
        */
        $tmp_array   = explode('->', $tmp);
        /* eg. $tmp_array = array(
            'server',
            'pgsql',
            'db_name',
        );
        */
        $main_string = $tmp_array[0]; //eg. $main_string = 'server';
        $r = $$main_string; //eg. $r = $server;
        unset($tmp_array[0]);
        foreach ($tmp_array as $v) {
            if (!isset($r[$v])) {
                throw new Conf_Exception('pear Conf error: undifined');
            }
            $r = $r[$v]; //eg. 1st $r = $r['pgsql']; 2nd $r = $r['db_name'];
        }
        return $r;
    }
}
?>
